// 本地词库
package models

import (
	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

// 本地词库
type LocalQALib struct {
	ID          string `json:"id" gorm:"column:id;primaryKey;type:int;size:64;comment:主键"`
	Name        string `json:"name" gorm:"column:name;type:string;size:30;not null;index:idx_lib_name,unique,priority:1;comment:本地词库名称"`
	MerchantID  string `json:"merchant_id" gorm:"column:merchant_id;type:string;size:30;not null;index:idx_lib_name,unique,priority:2;"`
	TotalAnswer int64  `json:"total_answer" gorm:"column:total_answer;type:int;size:64;comment:答案合计"`
	CreatedAt   int64  `json:"-" gorm:"column:created_at;autoCreateTime;type:int;size:64;comment:创建时间"`
	UpdatedAt   int64  `json:"-" gorm:"column:updated_at;autoUpdateTime;type:int;size:64;comment:更新时间"`
}

// 表名
func (LocalQALib) TableName() string {
	return "local_qa_lib"
}

// BeforeCreate 自增id
func (l *LocalQALib) BeforeCreate(tx *gorm.DB) (err error) {
	if l.ID != "" {
		return nil
	}
	// 用户id为空时，自动添加用户id
	uuid, err := helper.UUID{}.String()
	if err != nil {
		return err
	}
	l.ID = uuid
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &LocalQALib{})
}

// LocalQALibKeyword 本地词库关键字
type LocalQAKeyword struct {
	ID        string `json:"id" gorm:"column:id;primaryKey;type:int;size:64;comment:主键"`
	LibID     string `json:"lib_id" gorm:"column:lib_id;type:string;size:30;index:idx_lib_keyword,priority:1;comment:位置id"`
	AnswerID  string `json:"answer_id" gorm:"column:answer_id;type:string;size:30;comment:答案id"`
	Keyword   string `json:"keyword" gorm:"column:keyword;type:string;size:20;index:idx_lib_keyword,priority:2;comment:关键字"`
	CreatedAt int64  `json:"-" gorm:"column:created_at;autoCreateTime;type:int;size:64;comment:创建时间"`
	UpdatedAt int64  `json:"-" gorm:"column:updated_at;autoUpdateTime;type:int;size:64;comment:更新时间"`
}

// 表名
func (LocalQAKeyword) TableName() string {
	return "local_qa_lib_keyword"
}

// BeforeCreate 自增id
func (l *LocalQAKeyword) BeforeCreate(tx *gorm.DB) (err error) {
	if l.ID != "" {
		return nil
	}
	// 用户id为空时，自动添加用户id
	uuid, err := helper.UUID{}.String()
	if err != nil {
		return err
	}
	l.ID = uuid
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &LocalQAKeyword{})
}

// LocationQALibAnswer 本地词库答案
type LocalQAAnswer struct {
	ID        string `json:"id" gorm:"column:id;primaryKey;type:int;size:64;comment:主键"`
	LibID     string `json:"lid_id" gorm:"column:lib_id;type:string;size:30;index:idx_lib_id;comment:位置id"`
	Answer    string `json:"answer" gorm:"column:answer;type:string;size:512;comment:答案"`
	CreatedAt int64  `json:"-" gorm:"column:created_at;autoCreateTime;type:int;size:64;comment:创建时间"`
	UpdatedAt int64  `json:"-" gorm:"column:updated_at;autoUpdateTime;type:int;size:64;comment:更新时间"`
}

// 表名
func (LocalQAAnswer) TableName() string {
	return "local_qa_lib_answer"
}

// BeforeCreate 自增id
func (l *LocalQAAnswer) BeforeCreate(tx *gorm.DB) (err error) {
	if l.ID != "" {
		return nil
	}
	// 用户id为空时，自动添加用户id
	uuid, err := helper.UUID{}.String()
	if err != nil {
		return err
	}
	l.ID = uuid
	return nil
}

// 模式自动注册
func init() {
	Models = append(Models, &LocalQAAnswer{})
}
